package com.dianrui.operation.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.dianrui.operation.Constant;
import com.dianrui.operation.R;
import com.dianrui.operation.okhttp.XHttpRequest;
import com.dianrui.operation.util.GlideUtil;
import com.dianrui.operation.util.SharedUtil;
import com.dianrui.operation.util.StringUtils;
import com.dianrui.operation.util.ToastUtil;
import com.dianrui.operation.util.Url;
import com.dianrui.operation.view.ninegride.NineView;
import com.dianrui.operation.view.ninegride.RatioImageView;
import com.google.gson.JsonObject;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 故障详情
 */
public class FaultDetailsActivity extends BaseActivity implements NineView.NineViewShowCallBack {
    private String orderCodes, numbers, title, contents, createTime, updateTimes;
    private String formname;
    private List<String> imageList = new ArrayList<>();
    private List<String> imgList= new ArrayList<>();
    private int maintain_id;

    @BindView(R.id.title)
    TextView titles;
    @BindView(R.id.order_code)
    TextView orderCode;
    @BindView(R.id.number)
    TextView number;
    @BindView(R.id.name)
    TextView name;
    @BindView(R.id.remark)
    TextView remark;
    @BindView(R.id.time)
    TextView time;
    @BindView(R.id.update_time)
    TextView updateTime;
    @BindView(R.id.top_right)
    TextView topRight;
    @BindView(R.id.nine)
    NineView nine;

    @Override
    public int getContentViewId() {
        return R.layout.activity_fault_details;
    }

    @Override
    public void initView() {
        titles.setText("故障详情");
        topRight.setVisibility(View.GONE);
        nine.maxCount(9).spacing(3).setNineViewShowCallBack(this).setUrlList(imageList);
        if (getIntent() != null && getIntent().getExtras() != null) {
            Bundle bundle=getIntent().getExtras();
            maintain_id=bundle.getInt("maintain_id");

        }
        getMainShow();
    }

    @Override
    public void initData() {
    }

    private void getMainShow() {
        JsonObject jsonObject=new JsonObject();
        jsonObject.addProperty("maintain_id",maintain_id);
        XHttpRequest.getInstance().postRequests(SharedUtil.getUrl() + Url.MAINSHOW, jsonObject.toString(), new XHttpRequest.XCallBack() {
            @Override
            public void onResponse(String result) {
                if (!StringUtils.isEmpty(result)) {
                    try {
                        JSONObject jsonObject = new JSONObject(result);
                        if (jsonObject != null &&!isFinishing()) {
                            if (Constant.DATASUCCESS.equals(jsonObject.optString("status"))) {
                                orderCode.setText(jsonObject.optJSONObject("data").optString("order_code"));
                                number.setText(jsonObject.optJSONObject("data").optString("number"));
                                name.setText(jsonObject.optJSONObject("data").optString("form_name"));
                                remark.setText(jsonObject.optJSONObject("data").optString("contents"));
                                time.setText(jsonObject.optJSONObject("data").optString("create_time"));
                                updateTime.setText(jsonObject.optJSONObject("data").optString("update_time"));
                                if (imageList != null) {
                                    imageList.clear();
                                }
                                JSONArray jsonArray = jsonObject.optJSONObject("data").optJSONArray("photo");
                                if (jsonArray.length() > 0) {
                                    for (int i = 0; i < jsonArray.length(); i++) {
                                        String str = jsonArray.optString(i);
                                        imageList.add(str);
                                    }
                                    nine.urlList(imageList);
                                    nine.setVisibility(View.VISIBLE);
                                }else {
                                    nine.setVisibility(View.GONE);
                                }
                            }else if ("1001".equals(jsonObject.optString("status"))) {
                                ToastUtil.showToast(jsonObject.optString("message"));
                            }else if ("1002".equals(jsonObject.optString("status"))) {
                                spUtils.clear();
                                ToastUtil.showToast(jsonObject.optString("message"));
                                JumpActivitys(LoginActivity.class);
                                finish();
                            }

                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void failedRequest(String result) {
                ToastUtil.showToast(getString(R.string.net_error));

            }
        });
    }


    @OnClick({R.id.back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            default:
                break;
        }
    }


    @Override
    public void onDisplayOneImage(RatioImageView imageView, String url, int parentWidth, Context context) {
        if ("default".equals(url)) {
        } else {
            imageView.setTag(R.id.glide_tag, url);
            addImageViewList(GlideUtil.downLoader(false, imageView, FaultDetailsActivity.this));
        }
    }

    @Override
    public void onDisplayImage(RatioImageView imageView, RatioImageView closeButton, String url, int parentWidth, Context context) {
        if ("default".equals(url)) {
        } else {
            imageView.setTag(R.id.glide_tag, url);
            addImageViewList(GlideUtil.downLoader(false, imageView, FaultDetailsActivity.this));
        }
    }

    @Override
    public void onClickImage(int position, String url, List<String> urlList, Context context) {
        List<String> temp = new ArrayList<>();
        int count = 0;
        for (int i = 0; i < imageList.size(); i++) {
            if (!"default".equals(imageList.get(i))) {
                temp.add(imageList.get(i));
            } else {
                count++;
            }
        }
        startActivity(new Intent(FaultDetailsActivity.this, BigImageActivity.class)
                .putStringArrayListExtra("data", (ArrayList<String>) temp)
                .putExtra("current", position));
    }

    @Override
    public void onClickImageColse(int position, String url, List<String> urlList, Context context) {

    }

    @Override
    public void onSamePhotos(List<String> mRejectUrlList) {

    }
}
